//
//  RankVC.swift
//  zhonglb_iOS
//
//  Created by lxm on 2021/12/22.
//

import UIKit

class RankVC: ZMViewController, LemonIndexHeaderViewDelegate {
    //MARK: --------------------属性--------------------
    let viewModel = RankVCViewModel()

    //MARK: --------------------UI控件--------------------
    var headerTopV = UIView().then { v in
        v.backgroundColor = .clear
    }
    
    let bgImg = ZMUtil.imageView(imageName: "red_bg")

    let qhbpm = UIImageView()

    let redBg = UIImageView()

    
    var heaerView = UIView()
    
    // 表格标题
    var tableTitleView = UIView().then { v in
        let label = ZMUtil.label(title: "排名", color: "#999999".zm.hex(), font: UIFont.systemFont(ofSize: 14))
        let user = ZMUtil.label(title: "用户", color: "#999999".zm.hex(), font: UIFont.systemFont(ofSize: 14))
        let dircet = ZMUtil.label(title: "红包金额", color: "#999999".zm.hex(), font: UIFont.systemFont(ofSize: 14))
        v.addSubview(label)
        v.addSubview(user)
        v.addSubview(dircet)
        label.snp.makeConstraints({$0.left.equalToSuperview().offset(18);$0.centerY.equalToSuperview();})
        user.snp.makeConstraints({$0.left.equalTo(label.snp.right).offset(26);$0.centerY.equalToSuperview();})
        dircet.snp.makeConstraints({$0.right.equalToSuperview().offset(-16);$0.centerY.equalToSuperview();})
        v.backgroundColor = .white
    }
    // 分栏选择
    var indexView = LemonIndexHeaderView().then { v in
        v.data = ["日榜", "月榜", "总榜"]
        v.layer.borderWidth = 0.5
        v.layer.borderColor = UIColor.white.cgColor
        v.layer.cornerRadius = 15
        v.lineView.frame = CGRect(x: 0, y: 0, width: 64, height: 30)
        v.lineView.backgroundColor = .white
    }
    
    // tableView
    let tableView = UITableView().then { t in
        t.separatorStyle = .none
    }
    
    // 前一天排行
    let lastRankBtn = ZMUtil.button(image:UIImage.init(named: "rank_last_day")).then { b in
        b.layer.shadowColor = Style.lineColor.cgColor
        b.layer.shadowOffset = CGSize(width: 0, height: 4)
        b.layer.shadowRadius = 12
        b.layer.shadowOpacity = 1
    }
        
    let showTimeLabel = ZMUtil.label(title: "", color: UIColor.green, font: UIFont.systemFont(ofSize: 14))
    
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()

        viewModel.loadRankList(range: 1)
    }
    //MARK: --------------------UI--------------------
    func setUI() {
        title = viewModel.type == 1 ? "抢红包排名" : "获得分享奖励排名"
        
        view.backgroundColor = .white
      
        indexView.delegate = self
        
        view.addSubview(bgImg)
        
        let config = LemonIndexHeaderView.LemonIndexHeaderConfig()
        config.selectTitleColor = Style.mainColor
        config.normalTitleColor = .white
        indexView.config = config
        

        headerTopV.addSubview(indexView)
        headerTopV.addSubview(qhbpm)
        headerTopV.addSubview(redBg)
        headerTopV.layer.masksToBounds = true
        
        qhbpm.image = UIImage.init(named: viewModel.type == 1 ? "red_qhbpm" : "red_fxjlpm")
        redBg.image = UIImage.init(named: viewModel.type == 1 ? "red_bg_icon" : "red_bag_icon2")
        
        
        heaerView.addSubview(headerTopV)
        heaerView.addSubview(tableTitleView)
        view.addSubview(heaerView)
        
        // table
        view.addSubview(tableView)
        tableView.zm.register(nibName: "RankCell", reuserId: "RankCell")
        tableView.rowHeight = 44
        viewModel.tableObj.setTable(tableView)
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.bounces = false
//        addRefresh(scrollView: tableView, addFoot: false) { [unowned self] ok in
//            self.viewModel.loadRankList(type: self.indexView.currentIndex + 1)
//            self.viewModel.loadRefreshTime()
//            self.viewModel.loadRankListAndTime(type: self.indexView.currentIndex + 1)
//        }
        
        view.addSubview(lastRankBtn)
        
        
        addNodataScrollView(scrollView: tableView)
        emptyTitle = "暂无天梯榜排行~"
        emptyImg = "rank_empty"
        
        view.addSubview(showTimeLabel)
        showTimeLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-50)
        }
        
        showTimeLabel.isHidden = developMode != 3
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        ZMUtil.clipCircle(view: tableTitleView, Corner: [.topLeft, .topRight], radius: 12)
    }
    
    func addConstraints() {
        
        bgImg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        qhbpm.snp.makeConstraints { make in
            make.top.equalTo(indexView.snp.bottom).offset(29)
            make.left.equalToSuperview().offset(16)
        }
        
        redBg.snp.makeConstraints { make in
            make.top.equalTo(indexView.snp.bottom).offset(16)
            make.right.equalToSuperview().offset(-16)
        }
        
        heaerView.snp.makeConstraints { make in
            make.height.equalTo(36 + 129)
            make.top.left.right.equalToSuperview()
        }
        
        headerTopV.snp.makeConstraints { make in
            make.height.equalTo(129)
            make.top.left.right.equalToSuperview()
        }
        
        indexView.snp.makeConstraints { make in
            make.width.equalTo(192)
            make.centerX.equalToSuperview()
            make.height.equalTo(30)
            make.top.equalToSuperview().offset(16)
        }
        
        tableTitleView.snp.makeConstraints { make in
            make.top.equalTo(headerTopV.snp.bottom)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview()
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(heaerView.snp.bottom)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(0)
        }
        
        lastRankBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-25)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        indexView.layoutIfNeeded()
        indexView.layoutSubviews()
        indexView.collectionView.reloadData()
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        viewModel.bindAction()
   
        
        // rank
        lastRankBtn.rx.tap.subscribe(onNext:{ [unowned self] resp in
            router.push("xm://main/Rank/last_rank/\(self.indexView.currentIndex + 1)")
        }).disposed(by: disposeBag)
     
    }
    
    //MARK: --------------------delegate--------------------
    func shouldSelect(indexView : LemonIndexHeaderView, indexpath: IndexPath) -> Bool {
        if indexView.currentIndex == indexpath.row {
            return false
        } else {
            let range = indexpath.row + 1
            viewModel.loadRankList(range: range){ flag in
                if flag {
                   indexView.RealLineViewScrolleToIndex(index: indexpath.row, animation: true)
                }
            }
        }
        return false
    }
}

extension RankVC {
    class InTroButton : UIButton {
        override func draw(_ rect: CGRect) {
            super.draw(rect)
            let path = UIBezierPath()
            let lineWidth : CGFloat = 1
            let halfLineWidth = lineWidth / 2
            path.lineWidth = lineWidth
            "#01956A".zm.hex().setFill()
            let radius = (rect.height - lineWidth) / 2
            path.move(to: CGPoint(x: rect.width, y: halfLineWidth))
            path.addLine(to: CGPoint(x: radius + halfLineWidth, y: halfLineWidth))
            path.addArc(withCenter: CGPoint(x: radius + halfLineWidth , y: radius + halfLineWidth), radius: radius, startAngle: 1.5 * CGFloat.pi  , endAngle: 0.5 * CGFloat.pi, clockwise: false)
            path.addLine(to: CGPoint(x: rect.width, y: rect.height - halfLineWidth))
            path.fill()
        }
    }
}
